package com.tgy.myplus.controller;

import com.tgy.myplus.entity.Student;
import com.tgy.myplus.service.StudentService;
import com.tgy.myplus.service.impl.StudentServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (Student)表控制层
 *
 * @author makejava
 * @since 2021-10-11 20:36:15
 */
@RestController
@Slf4j
public class StudentController {
    @Resource
    StudentService studentService;
    @Resource
    StudentServiceImpl studentServiceImpl;

    /**
     * 查询所有
     * @return 列表
     */
    @PostMapping("querryAll")
    public List<Student> querryAll (){
        return this.studentService.querryAll();
    }

    /**
     * 增加一条记录
     * @param student
     * @return 改变行数
     */
    @PostMapping("insert")
    public int insert (@RequestBody Student student){
        return this.studentService.insert(student);
    }

    @PostMapping("delete")
    public int delete(@RequestBody Student student){
        return this.studentServiceImpl.delete(student);
    }

    @PostMapping("update")
    public int update(@RequestBody Student student){
        return this.studentServiceImpl.update(student);
    }

    @PostMapping("queryByEntity")
    public List<Student> queryByEntity(@RequestBody Student student){
        return this.studentServiceImpl.queryByEntity(student);
    }

    @RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE})
    public String query(HttpServletRequest request, HttpEntity<String> requestEntity) {

        HttpMethod requestMethod = HttpMethod.resolve(request.getMethod());

        if (HttpMethod.GET == requestMethod) {
            log.info("request method is {}, query path is {}", request.getMethod(), request.getRequestURL() + "?" + request.getQueryString());
        } else if (HttpMethod.POST == requestMethod) {
            log.info("request method is {}, query path is {}, request body is {}", request.getMethod(), request.getRequestURL(), requestEntity.getBody());
        }

        RestTemplate restTemplate = new RestTemplate();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        requestEntity = new HttpEntity<>(requestEntity.getBody(), headers);

        ResponseEntity<String> response = null;
        try {
            response = restTemplate.exchange(
                    "http://136.192.125.87:9902/sms/queryAllMessage" + (HttpMethod.GET == requestMethod ? ("?" + request.getQueryString()) : ""),
                    requestMethod,
                    requestEntity,
                    String.class);
        } catch (RestClientException e) {
            log.error(e.toString());
            throw e;
        }

        return response.getBody();
    }

}
